<?php

require_once 'cms/content/om/om/BaseContentI18n.php';


/**
 * Skeleton subclass for representing a row from the 'content_i18n' table.
 *
 * 
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package cms.content.om
 */	
class ContentI18n extends BaseContentI18n {

    private $count;

    private $usedLanguages;


    public function getCount() {
        return $this->count;
    }

    public function setCount($count) {
        $this->count = $count;
    }

    public function hydrate(ResultSet $rs, $startcol = 1)
    {
        $startcol = parent::hydrate($rs, $startcol);

        try {
            $this->count = $rs->getInt(++$startcol);
        } catch(Exception $e) {
            ;
        }
    }

    
    /**
     * Add to search index
     */
    function addToIndex($indexPath) {
    	
    	// Update Lucene index
        require_once("util/Search.php");

        $search = new Search($indexPath);
        $search->addDocument(
            "content_".$this->getContentI18nId(),   // UID      ( indexed )
            $this->getName(),                       // Title    ( tokenized & indexed )
   			$this->getIntro(),						// Summary  ( stored )
            $this->getBody(),                       // Body     ( tokenized )
            $this->getLanguageCode(),               // Language ( indexed )
            $this->getContent()->getCategory()
                ->getUri()."/".$this->getContent()->getUri()   // URL      ( stored )
        );
        $search->optimize();
    	
    }

    /**
     * Does child contentI18n have a sibling with the
     * specified language code ?
     *
     * @param code The language code
     */
    function hasLanguage($code) {
        $c = new Criteria();
        $c->add(ContentI18nPeer::CONTENT_ID, $this->getContentId());
        $c->add(ContentI18nPeer::LANGUAGE_CODE, $code);
        return ContentI18nPeer::doCount($c) > 0;
    }


    /**
     * Get the already used languages for this contentI18n.
     *
     */
    public function getUsedLanguages() {
        if(is_null($this->usedLanguages)) {
            $criteria = new Criteria();
            $criteria->clearSelectColumns()->clearOrderByColumns();
            $criteria->addSelectColumn(ContentI18nPeer::LANGUAGE_CODE);
            $criteria->add(ContentI18nPeer::CONTENT_ID, $this->getContentId());
            $rs = ContentI18nPeer::doSelectRS($criteria);
            $codes = array();
            while($rs->next())
                $codes[] = $rs->getString(1);
            $this->usedLanguages = $codes;
        }
        return $this->usedLanguages;
    }


    /**
     * Get the available languages for this contentI18n.
     *
     */
    public function getAvailableLanguages() {
        if(is_null($this->usedLanguages)) {
            $used = $this->getUsedLanguages();
            $criteria = new Criteria();
            $criteria->add(LanguagePeer::CODE, $used, Criteria::NOT_IN);
            $criteria->add(LanguagePeer::I18N, true);
            $criteria->addAscendingOrderByColumn(LanguagePeer::NAME);
            $this->usedLanguages = LanguagePeer::doSelect($criteria);
        }
        return $this->usedLanguages;
    }


    /**
     * Get the name of this contentI18n in the CMS default language.
     *
     * @param default The default CMS language code.
     */
    function getDefaultName($default) {
        if($this->getLanguageCode() == $default)
            return $this->getName();
        else {
            $c = new Criteria();
            $c->clearSelectColumns();
            $c->addSelectColumn(ContentI18nPeer::NAME);
            $c->add(ContentI18nPeer::CONTENT_ID, $this->getContentId());
            $c->add(ContentI18nPeer::LANGUAGE_CODE, $default);
            $c->setLimit(1);
            $rs = ContentI18nPeer::doSelectRs($c);
            if ($rs->next())
                return $rs->getString(1);
        }
    }


    /**
     * Get the not default language contentI18ns with the same content_id
     *
     * @param default The default CMS language code.
     */
    public function getForeignContent18ns($default) {

        $c = new Criteria();
        $c->add(ContentI18nPeer::CONTENT_ID, $this->getContentId());
        $c->add(ContentI18nPeer::LANGUAGE_CODE, $default, Criteria::NOT_EQUAL);
        return ContentI18nPeer::doSelect($c);

    }

    /*
     * OVERRIDE THIS FROM THE BASE CLASS. OTHERWISE POPULATING FORM WILL NOT WORK IN ACTIONCONTROLLER
     * WHEN FOREIGN KEY OBJECT ARE USED
     */
    public function setContentId($v)
    {
        if ($this->content_id !== $v) {
            $this->content_id = $v;
            $this->modifiedColumns[] = ContentI18nPeer::CONTENT_ID;
        }
    }

} // ContentI18n
